using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Windows.Forms;
using ESecretary.OA;
using ESecretary.MyCommon;
using System.Net;
using ESecretary.OA.Service;
using System.Linq;

namespace ESecretary.Business
{
    public class UserBO
    {
        private OADelegate service;
        private TreeView _tv;

       


        public UserBO(TreeView tv)
        {
            _tv = tv;
            service = OADelegate.Create();
            
        }

        public void BindDeptUser()
        {
            List<DeptUserInfo> list = service.GetDeptUsers();
            _tv.Nodes.Clear();
            BindNode(_tv, list, null, 0);
        }

  

   

        public void UpdateUserStatus(List<int> onlineUserIDs)
        {            
            UpdateNode(_tv.Nodes[0],onlineUserIDs); 
        }

        private void UpdateNode(TreeNode node, List<int> onlineUserIDs)
        {
            int userID = (int)node.Tag;
            if (userID < 0)
            {
                userID = Math.Abs(userID);
                if (onlineUserIDs.IndexOf(userID) < 0)
                {
                    if (node.ImageKey != "offline.gif")
                    {
                        node.ImageKey = "offline.gif";
                        node.SelectedImageKey = "offline.gif";
                    }
                }
                else
                {
                    if (node.ImageKey != "online.gif")
                    {
                        node.ImageKey = "online.gif";
                        node.SelectedImageKey = "online.gif";
                    }                   
                }
            }
            foreach (TreeNode tmp in node.Nodes)
            {
                UpdateNode(tmp, onlineUserIDs);
            }
        }

        private void BindNode(TreeView tv, List<DeptUserInfo> dt, TreeNode node, int upperID)
        {
            IEnumerable<DeptUserInfo> rows = dt.Where(T => T.UpperID == upperID);

            foreach (DeptUserInfo dr in rows)
            {
                TreeNode ntmp = new TreeNode();
                ntmp.Text = dr.Name;
                ntmp.Tag = dr.ID;
             
                if (dr.ID > 0)
                {
                    ntmp.ImageKey = "collapse.gif";
                    ntmp.SelectedImageKey = "collapse.gif";                   
                }
                else
                {
                    //int userID = Math.Abs(dr.ID);
                    //if (MyCommon.GlobalVar.OnlineUsers.IndexOf(userID) < 0)
                    //{
                        ntmp.ImageKey = "offline.gif";
                        ntmp.SelectedImageKey = "offline.gif";
                    //}
                    //else
                    //{
                    //    ntmp.ImageKey = "online.gif";
                    //    ntmp.SelectedImageKey = "online.gif";
                    //}
                }

                if (node == null)
                {                    
                    tv.Nodes.Add(ntmp);
                    ntmp.Expand();
                }
                else
                {
                    node.Nodes.Add(ntmp);
                }
                BindNode(tv, dt, ntmp, dr.ID);
            }
        }

        internal List<DeptUserInfo> GetData()
        {
            List<DeptUserInfo> dt = service.GetDeptUsers();
            return dt;
        }
    }
}
